package flca.mda.api.util;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * Helper class to wrap a Field, so that also not explicit Id fields can be supported
 * @author robin
 *
 */
public class FieldWrapper {
	private static TypeUtils tu = new TypeUtils();
	
	private Field field; // a normal field
	private MockField mockfield; // used for the Id if there is no explicit Id field defined
	
	public String getName() {
		if (field != null) return field.getName();
		else return mockfield.getName();
	}

	public Class<?> getType() {
		if (field != null) return field.getType();
		else return mockfield.getType();
	}

	public Type getGenericType() {
		if (field != null) return field.getGenericType();
		else return null;
	}
	
	/**
	 * return the name of the actual type or generic type it is a collection
	 * @return
	 */
	public String getTypeName() {
		if (field != null) {
			return tu.getGenericOrActualType(field).getSimpleName();
		} else {
			return mockfield.getType().getSimpleName();
		}
	}
	
	public boolean isField() {
		return (field != null);
	}

	public boolean isId() {
		if (field != null) return tu.isIdField(field);
		else return mockfield.isId();
	}
	
	public boolean isEnum() {
		if (field != null) return tu.isEnum(field);
		else return false;
	}
	
	
	public boolean isRequired() {
		if (field != null) return tu.isRequired(field);
		else return true;
	}
	
	public boolean isManyToOneField() {
		if (field != null) return tu.isManyToOneField(field);
		else return false;
	}

	public boolean isManyToManyField() {
		if (field != null) return tu.isManyToManyField(field);
		else return false;
	}

	public boolean isOneToManyField() {
		if (field != null) return tu.isOneToManyField(field);
		else return false;
	}

	public boolean isOneToOneField() {
		if (field != null) return tu.isOneToOneField(field);
		else return false;
	}

	public boolean isSimpleField() {
		if (field != null) return tu.isSimpleField(field);
		else return false;
	}


	public String getColumnName() {
		if (field != null) return tu.getColumnName(field);
		else return mockfield.getName();
	}
	
	public FieldWrapper(Field field) {
		super();
		this.field = field;
	}

	public FieldWrapper(MockField mockfield) {
		super();
		this.mockfield = mockfield;
	}

	public Field getField() {
		return field;
	}

	public MockField getIdfield() {
		return mockfield;
	}

	@Override
	public String toString() {
		return "FieldWrapper [name= " + getName() + "field=" + field + ", mockfield=" + mockfield + "]";
	} 
	
	
	
	
}

